package com.h3c.nem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h3c.nem.dto.PageQueryDTO;
import com.h3c.nem.entity.TbCareNoticeSetting;
import com.h3c.nem.mapper.TbCareNoticeSettingMapper;
import com.h3c.nem.service.ITbCareNoticeSettingService;
import com.h3c.nem.utils.AppBusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 新员工关怀通知配置
 *
 * @author z22968
 * @date 2022/6/8 15:17
 */
@Slf4j
@Service
public class TbCareNoticeSettingServiceImpl extends ServiceImpl<TbCareNoticeSettingMapper, TbCareNoticeSetting> implements ITbCareNoticeSettingService {
    @Resource
    TbCareNoticeSettingMapper tbCareNoticeSettingMapper;

    @Override
    public void insert(TbCareNoticeSetting tbCareNoticeSetting) {
        if (tbCareNoticeSetting.getType() == null) {
            throw new AppBusinessException("类型不能为空");
        }
        if (!StringUtils.hasText(tbCareNoticeSetting.getTextValue())) {
            throw new AppBusinessException("内容不能为空");
        }
        if (!tbCareNoticeSetting.getType().equals(TbCareNoticeSetting.SettingType.PRE_STATEMENT)) {
            throw new AppBusinessException("只能添加预设鼓励语句");
        }
        tbCareNoticeSetting.setName(TbCareNoticeSetting.SettingType.PRE_STATEMENT.getName());
        this.save(tbCareNoticeSetting);
    }

    @Override
    public void update(TbCareNoticeSetting tbCareNoticeSetting) {
        if (!StringUtils.hasText(tbCareNoticeSetting.getId())) {
            throw new AppBusinessException("id不能为空");
        }
        TbCareNoticeSetting tmp = this.getBaseMapper().selectById(tbCareNoticeSetting.getId());
        if (tmp == null) {
            throw new AppBusinessException("不存在该数据");
        }
        if (tbCareNoticeSetting.getValue() == null && !StringUtils.hasText(tbCareNoticeSetting.getTextValue())) {
            throw new AppBusinessException("值不能为空");
        }
        if (tmp.getType().equals(TbCareNoticeSetting.SettingType.FREQUENCY)) {
            if ("校招其他".equals(tmp.getName()) || "绩效优秀".equals(tmp.getName())) {
                if (tbCareNoticeSetting.getValue() > 28) {
                    throw new AppBusinessException("请选择28号之前的日期");
                }
            }
        }
        if (tmp.getType().equals(TbCareNoticeSetting.SettingType.PEOPLE_NUM) || tmp.getType().equals(TbCareNoticeSetting.SettingType.FREQUENCY)) {
            tmp.setValue(tbCareNoticeSetting.getValue());
        } else {
            tmp.setTextValue(tbCareNoticeSetting.getTextValue());
        }
        this.updateById(tmp);
    }

    @Override
    public TbCareNoticeSetting selectOne(TbCareNoticeSetting.SettingType type, String name) {
        return getOne(new LambdaQueryWrapper<TbCareNoticeSetting>()
                .eq(TbCareNoticeSetting::getType, type)
                .eq(TbCareNoticeSetting::getName, name));
    }

    @Override
    public List<TbCareNoticeSetting> selectByType(String type) {
        return lambdaQuery().eq(TbCareNoticeSetting::getType, type).list();
    }

    @Override
    public PageInfo<TbCareNoticeSetting> selectByPage(PageQueryDTO<TbCareNoticeSetting> pageQuery) {
        Page<TbCareNoticeSetting> page = PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        TbCareNoticeSetting tbCareNoticeSetting = pageQuery.getCondition();
        LambdaQueryWrapper<TbCareNoticeSetting> queryWrapper = new LambdaQueryWrapper<>();
        if (tbCareNoticeSetting.getType() == null) {
            // 默认查询人数+频率
            queryWrapper.or(tmp -> {
                tmp.eq(TbCareNoticeSetting::getType, TbCareNoticeSetting.SettingType.PEOPLE_NUM);
                tmp.or().eq(TbCareNoticeSetting::getType, TbCareNoticeSetting.SettingType.FREQUENCY);
            });
        } else if (tbCareNoticeSetting.getType().equals(TbCareNoticeSetting.SettingType.MAIL_CONTENT) || tbCareNoticeSetting.getType().equals(TbCareNoticeSetting.SettingType.WECHAT_CONTENT)) {
            // 默认查询企业微信+邮件
            queryWrapper.or(tmp -> {
                tmp.eq(TbCareNoticeSetting::getType, TbCareNoticeSetting.SettingType.MAIL_CONTENT);
                tmp.or().eq(TbCareNoticeSetting::getType, TbCareNoticeSetting.SettingType.WECHAT_CONTENT);
            });
        } else {
            // 添加类型条件
            queryWrapper.eq(TbCareNoticeSetting::getType, tbCareNoticeSetting.getType());
        }
        // 添加name条件
        if (StringUtils.hasText(tbCareNoticeSetting.getName())) {
            queryWrapper.eq(TbCareNoticeSetting::getName, tbCareNoticeSetting.getName());
        }
        queryWrapper.orderByAsc(TbCareNoticeSetting::getType);
        List<TbCareNoticeSetting> list = tbCareNoticeSettingMapper.selectList(queryWrapper);
        return page.toPageInfo();
    }

    @Override
    public void deleteById(String id) {
        TbCareNoticeSetting tbCareNoticeSetting = tbCareNoticeSettingMapper.selectById(id);
        if (!tbCareNoticeSetting.getType().equals(TbCareNoticeSetting.SettingType.PRE_STATEMENT)) {
            throw new AppBusinessException("只能删除预设鼓励语句");
        }
        tbCareNoticeSettingMapper.deleteById(id);
    }
}
